
const myPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("初始值"); // 模拟成功
        // 或者 reject
    }, 1000);
});
const handleFulfilled = (value) => {
    console.log("处理成功:", value);
    return value + "bar";  // 传递给下一个then
};

const handleRejected = (err) => {
    console.error("处理失败:", err);
    throw err;  // 或返回新的Promise
};

myPromise
    .then(handleFulfilled, handleRejected)
    .then(handleFulfilled, handleRejected)
    .catch(err => console.error("捕获所有错误:", err));

//使用箭头函数
myPromise
    .then((value) => `${value} and bar`)
    .then((value) => `${value} and bar again`)
    .then((value) => `${value} and again`)
    .then((value) => `${value} and again`)
    .then((value) => {
        console.log(value);
    })
    .catch((err) => {
        console.error(err);
    });

//延迟200ms
function timeout(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, ms, 'done');
    });
}

timeout(200).then((value) => {
    console.log(value);
});


let promise = new Promise(function(resolve, reject) {
    console.log('Promise');
    resolve();
});

promise.then(function() {
    console.log('resolved');
});

console.log('Hi!');

// Promise
// Hi!
// resolved


// 延迟1秒后输出
setTimeout(() => console.log('Hello'), 1000);
// 立即输出并延迟1秒后输出
setTimeout(() => console.log('World'), 0);

